/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { beforeAll, describe, expect, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from './common/testCommon';
import path from 'path';
import { Rule } from '../../../src/Index';
import { CheckEntry } from '../../../src/utils/common/CheckEntry';
import { AVPlayBackStateCheck } from '../../../src/checker/performance/AVPlayBackStateCheck';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
    const rule: Rule = new Rule('');
    checkEntry = await testCaseCheck('./test/unittest/sample/AVPlayBackState', rule, CHECK_MODE.PROJECT2CHECK, AVPlayBackStateCheck);
    realPath = checkEntry.scene.getRealProjectDir();
})

describe('AVPlayBackStateCheckTest', () => {
    test('AVPlayBackStateCheckTest_001_Report', () => {
        const detectFile: string = path.join(realPath, 'ets', 'AVPlayBackStateReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(1);
        
        // 验证告警内容
        const report = detectFileReports[0];
        expect(report.defect.description).toContain('When state is PLAYBACK_STATE_PLAY, position should not be null or undefined');
        
        // 打印实际的行列号用于调试
        console.log(`Report 001: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        
        // 验证告警的行列号
        expect(report.defect.reportLine).toBe(21);
        expect(report.defect.reportColumn).toBe(15);
        expect(report.defect.fixKey).toBe('21%15%24%');
    });

    test('AVPlayBackStateCheckTest_002_NoReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'AVPlayBackStateNoReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(0);
    });

    test('AVPlayBackStateCheckTest_003_NoPositionReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'AVPlayBackStateNoPositionReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(1);
        
        // 验证告警内容
        const report = detectFileReports[0];
        expect(report.defect.description).toContain('When state is PLAYBACK_STATE_PLAY, position should not be null or undefined');
        
        // 打印实际的行列号用于调试
        console.log(`NoPosition report: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        
        // 验证告警的行列号
        expect(report.defect.reportLine).toBe(20);
        expect(report.defect.reportColumn).toBe(12);
        expect(report.defect.fixKey).toBe('20%12%62%');
    });

    test('AVPlayBackStateCheckTest_004_ComplexReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'AVPlayBackStateComplexReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        // 3个应该告警：position 为 null、position 缺失、position 为 undefined
        expect(detectFileReports.length).toBe(3);
        
        // 打印实际的行列号用于调试
        detectFileReports.forEach((report, index) => {
            console.log('实际告警:', index, report.defect.reportLine, report.defect.reportColumn, report.defect.fixKey);
        });
        
        // 验证告警内容和行列号
        const expectedLines = [20, 28, 37]; // 20: position:null, 28: 缺失, 37: undefined
        const expectedColumns = [12, 12, 15];
        const expectedFixKeys = ['20%12%62%', '28%12%13%', '37%15%24%'];
        
        detectFileReports.forEach((report, index) => {
            expect(report.defect.description).toContain('When state is PLAYBACK_STATE_PLAY, position should not be null or undefined');
            expect(report.defect.reportLine).toBe(expectedLines[index]);
            expect(report.defect.reportColumn).toBe(expectedColumns[index]);
            expect(report.defect.fixKey).toBe(expectedFixKeys[index]);
        });
    });

    test('AVPlayBackStateCheckTest_005_MixedReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'AVPlayBackStateMixedReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) => 
            issue.defect.mergeKey.startsWith(detectFile)
        );
        // 只有2个应该告警：第1个和第4个（position 为 null 和 position 缺失）
        expect(detectFileReports.length).toBe(2);
        
        // 打印实际的行列号用于调试
        detectFileReports.forEach((report, index) => {
            console.log(`Mixed report ${index}: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        });
        
        // 验证告警内容和行列号
        const expectedLines = [20, 49]; // 20: position:null, 49: speed(缺失)
        const expectedColumns = [12, 12];
        const expectedFixKeys = ['20%12%62%', '49%12%62%'];
        
        detectFileReports.forEach((report, index) => {
            expect(report.defect.description).toContain('When state is PLAYBACK_STATE_PLAY, position should not be null or undefined');
            expect(report.defect.reportLine).toBe(expectedLines[index]);
            expect(report.defect.reportColumn).toBe(expectedColumns[index]);
            expect(report.defect.fixKey).toBe(expectedFixKeys[index]);
        });
    });
})
